package dk.hewison.client.album;


import dk.hewison.client.AppEvent;
import static dk.hewison.client.AppEvent.Event.ALBUM_SELECTED;
import dk.hewison.client.data.Callback;
import dk.hewison.client.data.DataController;
import dk.hewison.client.domain.Directory;
import dk.hewison.client.mvc.AbstractController;
import dk.hewison.client.mvc.View;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */

public class AllAlbumPageController extends AbstractController<Directory> {


    public AllAlbumPageController(int id) {
        super(id);

        registerEvent(ALBUM_SELECTED);
    }


    public void eventOccurred(AppEvent event) {
        switch (event.getEvent()) {
            case ALBUM_SELECTED:
                doAlbumSelected((Directory) event.getObject());
                break;
        }
    }


    @Override
    public void showPage() {
        DataController.getInstance().getDirectory(null, new Callback<Directory>() {
            @Override
            public void doSuccess(Directory result) {
                setModel(result);
            }
        });

    }

    public void setModel(Directory directoryList) {
        view.setModel(directoryList);
    }

    public Directory getModel() {
        return null;
    }

    public View<Directory> createView() {
        //todo move to factory
        return new AlbumPageView();
    }

    private void doAlbumSelected(Directory directory) {
        System.out.println("AllAlbumPageController.doAlbumSelected " + directory);
        loadDirectory(directory);
    }

    private void loadDirectory(Directory directory) {
        DataController.getInstance().getDirectory(directory, new Callback<Directory>() {
            @Override
            public void doSuccess(Directory result) {
                setModel(result);
            }
        });
    }

}
